package com.baizhi.controller;

import com.baizhi.entity.Student;
import com.baizhi.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("add")
    public Map add(Student student){
        System.out.println("add收参"+student);
        Map map = new HashMap();
        try {
            studentService.insert(student);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("delete")
    public Map delete(int id){
        Map map = new HashMap();
        try {
            studentService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("update")
    public Map update(Student student){
        Map map = new HashMap();
        try {
            studentService.update(student);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    public Student getOne(int id){
        Student student = studentService.selectById(id);
        return student;
    }
    @RequestMapping("select")
    public Map select(int page,int limit){
        Map map = new HashMap();
        PageInfo<Student> pageInfo = studentService.select(page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
}
